# -*- coding: utf-8 -*-
import pygame
from time import sleep
from sys import exit

bian=8
class qizi:
    def __init__(self):
        self.posh=[]
        self.posb=[]
        self.bai=pygame.image.load(r'image\bai.png').convert_alpha()
        self.hei=pygame.image.load(r'image\hei.png').convert_alpha()
        self.bais=pygame.image.load(r'image\bais.png').convert_alpha()
        self.heis=pygame.image.load(r'image\heis.png').convert_alpha()
        self.ping=pygame.image.load(r'image\ping.png').convert_alpha()
        self.tag=1
    def win(self):
        if len(self.posb)>=5:
            for (i,j) in self.posb:
                if (((i-2,j-2) in self.posb) and ((i-1,j-1) in self.posb) and ((i+1,j+1) in self.posb) and( (i+2,j+2) in self.posb))or(((i-2,j+2) in self.posb) and ((i-1,j+1) in self.posb) and ((i+1,j-1) in self.posb) and ((i+2,j-2) in self.posb))or                  (((i-2,j) in self.posb) and ((i-1,j) in self.posb) and ((i+1,j) in self.posb) and ((i+2,j) in self.posb))or                  (((i,j-2) in self.posb) and ((i,j-1) in self.posb) and ((i,j+1) in self.posb) and ((i,j+2) in self.posb)):
                    self.posb=[]
                    self.posh=[]
                    self.tag=1
                    return 1
            for (i,j) in self.posh:
                if (((i-2,j-2) in self.posh) and ((i-1,j-1) in self.posh) and ((i+1,j+1) in self.posh) and ((i+2,j+2) in self.posh) )or(((i-2,j+2) in self.posh) and ((i-1,j+1) in self.posh) and ((i+1,j-1) in self.posh) and ((i+2,j-2) in self.posh))or(((i-2,j) in self.posh) and ((i-1,j) in self.posh) and ((i+1,j) in self.posh) and ((i+2,j) in self.posh))or(((i,j-2) in self.posh) and ((i,j-1) in self.posh) and ((i,j+1) in self.posh) and ((i,j+2) in self.posh)) :
                    self.posb=[]
                    self.posh=[]
                    self.tag=1
                    return 0
    def down(self):
        if self.tag==64:
            screen.blit(self.ping,(bian,90))
            pygame.display.update()
            self.tag=1
            sleep(2)
            return;
        x,y=pygame.mouse.get_pos()
        x=int(x/50)*50
        y=int(y/50)*50
        xx=int(x/50)
        yy=int(y/50)
        if not( ((xx,yy) in self.posh) or ((xx,yy) in self.posb)):
            if self.tag%2==0:
                screen.blit(self.hei,(x,y))
                self.posh.append((xx,yy))
                self.tag=self.tag+1
            else:
                screen.blit(self.bai,(x,y))
                self.posb.append((xx,yy))
                self.tag=self.tag+1
            print(self.tag,self.posb,xx,yy,x,y)
        else:
            pass
def wzq():        
    pygame.init()
    pygame.display.set_caption('五子棋')
    screen.blit(background,(0,0))
    tag=1
#    move(2,0)
    return
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                exit()
            if event.type==pygame.MOUSEBUTTONDOWN:
                sleep(2)
                qi.down()
                pygame.display.update()
                t=qi.win()
                if t==1:
                    screen.blit(qi.bais,(bian,90))
                    pygame.display.update()
                    sleep(2)
                    screen.blit(background,(0,0))
                    break
                elif t==0:
                    screen.blit(qi.heis,(bian,90))
                    pygame.display.update()
                    #print('ss')
                    sleep(2)
                    screen.blit(background,(0,0))
                    break
                
         
        pygame.display.update()
def move(ly,lx):
        ly=7-ly
        if qi.tag==64:
            screen.blit(qi.ping,(bian,90))
            pygame.display.update()
            qi.tag=1
            sleep(2)
            return;
        x,y=lx*50,ly*50
        x=int(x/50)*50
        y=int(y/50)*50
        xx=int(x/50)
        yy=int(y/50)
        if not( ((xx,yy) in qi.posh) or ((xx,yy) in qi.posb)):
            if qi.tag%2==0:
                screen.blit(qi.hei,(x,y))
                qi.posh.append((xx,yy))
                qi.tag=qi.tag+1
            else:
                screen.blit(qi.bai,(x,y))
                qi.posb.append((xx,yy))
                qi.tag=qi.tag+1
            #print(qi.tag,qi.posb,xx,yy,x,y)
        else:
            pass
        pygame.display.update()
        t=qi.win()
        if t==1:
            screen.blit(qi.bais,(bian,90))
            pygame.display.update()
            sleep(2)
            screen.blit(background,(0,0))
        elif t==0:
            screen.blit(qi.heis,(bian,90))
            pygame.display.update()
            #print('ss')
            sleep(2)
            screen.blit(background,(0,0))
def get_action():
    while True:
        for event in pygame.event.get():
            #print event.type
            #sleep(2)
            if event.type==pygame.MOUSEBUTTONDOWN:
                x,y=pygame.mouse.get_pos()
                #print x,y
                return 7-y/50,x/50
        pygame.display.update()
screen=pygame.display.set_mode((400,400),0,32)
background=pygame.image.load(r'image\qipan.png').convert()
qi=qizi()
wzq()
#move(2,0)
#get_action()
